package com.example.huainanlive.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.huainanlive.R
import com.example.huainanlive.adapter.LiveChartAdapter
import com.example.huainanlive.databinding.FragmentLiveChartBinding


class LiveChartFragment: BaseBindingViewFragment<FragmentLiveChartBinding>(R.layout.fragment_live_chart)  {
    private lateinit var huiGuLiveViewModel: HuiGuLiveViewModel
    private lateinit var liveChartAdapter: LiveChartAdapter
    private var mWsViewModel: WsViewModel? = null

    override fun initBinding(layoutInflater: LayoutInflater): FragmentLiveChartBinding {
        return FragmentLiveChartBinding.inflate(layoutInflater)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        huiGuLiveViewModel = ViewModelProvider(
            requireActivity()
        )[HuiGuLiveViewModel::class.java]
        mWsViewModel = ViewModelProvider(
            requireActivity(),
            WsViewModel.WsViewModelModelFactory()
        )[WsViewModel::class.java]

        //添加观众留言
        mWsViewModel?.addMessage?.observe(viewLifecycleOwner){
            it.t?.let { data ->
                liveChartAdapter.addData(data)
            }
            binding.rvLiveChart.smoothScrollToPosition(liveChartAdapter.data.size-1)
        }
        liveChartAdapter= LiveChartAdapter()
        binding.rvLiveChart.layoutManager = LinearLayoutManager(activity)
        binding.rvLiveChart.adapter = liveChartAdapter

        //主持人信息
        huiGuLiveViewModel.getMessageBean.observe(viewLifecycleOwner){
            liveChartAdapter.setList(it)
            binding.rvLiveChart.smoothScrollToPosition(it.size-1)

        }
    }
}